//
//  CPStatusViewModel.swift
//  weibo
//
//  Created by zcp on 16/5/16.
//  Copyright © 2016年 zcp. All rights reserved.
//

import UIKit
///获取微博数据
class CPStatusViewModel: NSObject {
    

    //单例
    static let sharedStatus: CPStatusViewModel = CPStatusViewModel()
    //防止外界通过()来创建这个对象
    private override init() {
        super.init()
    }
    ///获取微博数据
    func loadStatus(callBack:(status: [CPStatus]?,error: NSError?)->()){
    
        if let access_token = CPUserAccountViewModel.sharedAccountViewModel.userAccount?.access_token{
            
            let urlSting = "2/statuses/home_timeline.json"
            let parameters = [
                
                "access_token" : access_token
            ]
            CPNetworkTool.sharedNetworkTool.request(requsetMethod.GET, URLString: urlSting, parameters: parameters, success: { (_, response) -> Void in
                //通过key:status得到想要微博数据
                if let dict = response as? [String : AnyObject]
                {
                    //创建一个空数组来装模型
                    var statusArr = [CPStatus]()
                    if let result = dict["statuses"] as? [[String: AnyObject]]
                    {
                    
                    //遍历数组中每一个字典
                    for dict in result
                    {
                        //字典转模型
                        let status = CPStatus(dict: dict)
                        //添加到数组
                        statusArr.append(status)

                    }
                    }
                    //回调数据到调用方
                    callBack(status: statusArr, error: nil)
                }
                
                
                }, failure: { (_, error) -> Void in
                    //数据加载失败
                    callBack(status: nil, error: error)
            })
        }
    }
}
